<?php

namespace app\common\logic;

use app\common\exception\LogicException;

class PhoneCodeLogic
{
    const KEY_PREFIX = 'code_';

    /**
     * @Description 获取手机验证码
     * @author: zj
     * @param string $phone
     * @param string $pictureCode
     * @param string $template
     * @time 2023/08/29 11:37:05
     * @return true
     */
    public function getCode(string $phone,string $pictureCode,string $template)
    {
        // 验证图形验证码
        $pictureCodeLogic = new PictureCodeLogic();
        $pictureCodeLogic->checkMathCode($pictureCode);

        $code = rand(1000,9999);
        $code= (string)$code;
        $key = self::KEY_PREFIX.$phone;
        // 验证码存入缓存，实效3分钟
        cache()->set($key,$code,180);

        // 短信发送验证码
//        $smaService = new A

        return true;
    }

    /**
     * @Description 检查手机验证码是否正确
     * @author: zj
     * @param string $phone
     * @param string $code
     * @time 2023/08/29 11:40:17
     * @return true
     */
    public function checkCode(string $phone,string $code)
    {
        $key = self::KEY_PREFIX.$phone;
        $value = cache()->get($key);
        if (!$value) {
            throw new LogicException("手机验证码有误");
        }
        if ($value != $code) {
            throw new LogicException("手机验证码有误");
        }

        // 验证通过，删除验证码缓存
        cache()->delete($key);

        return true;
    }
}